"use strict";

let Router = require('koa-router');
let config = require('../../../config/config.js');

let daoDispatcher = require('../../../dao/district.js');
let debug = require('debug')('district');

console.log('router:' + config.baseUrl + '/v2/district');
let router = new Router({
    prefix: config.baseUrl + '/v2/district'
});

router.get('/test', function*() {
    this.body = 'test work fine';
});

/**
 * 插入单个地区 /api/v2/district/
 {
    "adCode": "450102",
    "areaCode": "14800",
    "cityCode": "0771",
    "name": "华强",
    "center": [108.312827, 22.821305],
    "subId": [],
    "level": "biz_area"
}
 */
router.post('/', function*() {
    // TODO params check
    debug('router', this.request.inParams);
    let result = yield daoDispatcher.addOne(this.request.inParams);
    this.body = result;
});

/**
 * 添加多个地区
 * post /api/v2/district/batch
 {
 "adCode": "450102",
 "areaCode": "14800",
 "cityCode": "0771",
 "name": "华强",
 "center": [108.312827, 22.821305],
 "level": "biz_area",
 "districts":[
     {
     "adCode": "450103",
     "areaCode": "14800",
     "cityCode": "0771",
     "name": "华强2",
     "center": [108.312827, 22.821305],
     "level": "biz_area",
     "districts":[
         {
         "adCode": "450104",
         "areaCode": "14800",
         "cityCode": "0771",
         "name": "华强3",
         "center": [108.312827, 22.821305],
         "level": "biz_area"
         }]
     }]
 }
 *
 */
router.post('/batch', function*() {
    // TODO params check
    let result = yield daoDispatcher.addMany(this.request.inParams);
    this.body = result;
});

/**
 // 高德数据接口
 {
     "ad_code": "450102",
     "area_code": "14800",
     "city_code": "0771",
     "name": "华强",
     "center": ["108.312827", "22.821305"],
     "pid": "450102",
     "subid": [],
     "level": "biz_area"
 }
 */
router.post('/gddata', function*() {
    function replace(item) {
        item.adCode = item.adcode;
        delete item.adcode;

        item.areaCode = item.areacode;
        delete item.areacode;

        item.cityCode = item.citycode;
        delete item.citycode;
    }

    let json = this.request.inParams[0];
    let list = [json];
    while (list.length > 0) {
        var item = list.shift();
        replace(item);
        if (item.districts) {
            item.districts.forEach(function (item) {
                list.push(item);
            });
        }
    }

    // TODO params check

    let result = yield daoDispatcher.addMany(this.request.inParams);
    this.body = result;
});


/**
 * 查找某个地区

 get /api/v2/district?id=xx 根据id查找
 get ?pid=xxx 根据pid查找
 get ?keyword=xxx 根据 关键字查找
 get ?qs=xxx 根据xxx模糊匹配
 get ?lng=xx&lat=xx&radius=xx 根据距离查找
 */

router.get('/', function*() {
    let inParams = this.request.inParams;
    let result;

    // id
    if (inParams.id) {
        result = yield daoDispatcher.findById({id: inParams.id});
        this.body = result;
    }

    // pid
    else if (inParams.pid) {
        if (inParams.pid === 'null') {
            inParams.pid = null;
        }
        result = yield daoDispatcher.findByPid({pid: inParams.pid});
        this.body = result;
    }

    // radius
    else if (inParams.lng && inParams.lat && inParams.radius) {
        let params = {
            lng: inParams.lng,
            lat: inParams.lat,
            radius: inParams.radius
        };
        result = yield daoDispatcher.findByDistance(params);
        this.body = result;
    }

    // keyword
    else if (inParams.keyword) {
        result = yield daoDispatcher.findByKeyword({keyword: inParams.keyword});
        this.body = result;
    }

    // quicksearch
    else if (inParams.qs) {
        result = yield daoDispatcher.quickSearch({keyword: inParams.keyword});
        this.body = result;
    }

    // error
    else {
        this.status = 400;
        this.body = 'only accept id or pid';
    }
});

/**
 * 修改名字
 */
router.put('/', function*() {
    let params = this.request.inParams;
    let result = yield daoDispatcher.updateName({id: params.id, name: params.name});
    this.body = result;
});

module.exports = router.routes();